package com.ndp.fb.mq.service.schedulerservice.adpublish;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.fbagent.service.AdCampaignGroupFBService;
import com.ndp.fb.fbagent.service.AdGroupFBService;
import com.ndp.fb.fbagent.service.AdSetFBService;
import com.ndp.fb.mq.api.service.schedulerservice.AdAccountsSchedulerService;
import com.ndp.fb.redis.api.RedisClient;
import com.ndp.fb.util.ThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by Evan on 2015/3/4 16:55.
 */
@Component
public class PublishAdCampCountAndAdSetCountService
{
    private static Logger logger = LoggerFactory.getLogger(PublishAdCampCountAndAdSetCountService.class);
    @Autowired
    AdAccountsSchedulerService adaccountsSchedulerService;
    @Autowired
    private RedisClient redisClient;
    @Reference
    private AdCampaignGroupFBService adCampaignGroupFBService;
    @Reference
    private AdSetFBService adSetFBService;
    @Reference
    private AdGroupFBService adGroupFBService;

    public void fetchAdCampCountAndAdSetCount(final Long accountId)
    {
        ThreadPool.execute(() -> {
            //获取token
            String token = adaccountsSchedulerService.getAdaccountsToken(accountId);
            if (token == null) {
                logger.info("PublishAdCampCountAndAdSetCountService get token failed! 帐号：" + accountId);
                return;
            }
            Long tempAdCampaignCount = adCampaignGroupFBService.getCampaignGroupsSummaryByAccount(token, accountId);
            Long tempAdSetCount = adSetFBService.getAdSetsSumaryByAccount(token, accountId);
            Long tempAdGroupCount = adGroupFBService.getAdGroupSumaryByAccount(token, accountId);
            redisClient.set("FBAdCampCount_" + accountId, tempAdCampaignCount);
            redisClient.set("FBAdSetCount_" + accountId, tempAdSetCount);
            redisClient.set("FBAdGroupCount_" + accountId, tempAdGroupCount);
        });
    }

}
